#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <stack>
#include <time.h>

using namespace std;

int n;
string equ;

vector<string> split(string s)
{
    int begin = 0;
    int pos = 0;
    vector<string> res;
    while ((pos = s.find("+", begin)) != string::npos)
    {
        string temp = s.substr(begin, pos - begin);
        res.push_back(temp);
        begin = pos + 1;
    }
    res.push_back(s.substr(begin));
    return res;
}

map<string, int> check(vector<string> res)
{
    map<string, int> resmp;
    int c;
    int startpos = 0;
    for (auto &s : res)
    {
        map<string, int> mp;
        if (s.find("(") == string::npos)
        {
            int c = 0;
            int i = 0;

            //读数字
            if (isdigit(s[0]))
            {
                c = s[0] - '0';
                for (i = 1; isdigit(s[i]); i++)
                {
                    c *= 10;
                    c += (s[i] - '0');
                }
            }
            else
            {
                c = 1;
            }

            //
            for (i; i + 1 < s.length(); i++)
            {
                //大写跟小写
                if (isupper(s[i]) && islower(s[i + 1]))
                {
                    char a[10];
                    a[0] = s[i];
                    a[1] = s[i + 1];
                    a[2] = '\0';
                    string name(a);
                    mp[name] = 0;
                }

                //大写跟大写
                if (isupper(s[i]) && isupper(s[i + 1]))
                {
                    char a[10] = {s[i]};
                    a[1] = '\0';
                    mp[string(a)] += 1;
                }

                //大写跟数字
                if (isupper(s[i]) && isdigit(s[i + 1]))
                {
                    int tempc = s[i + 1] - '0';
                    for (int j = i + 2; isdigit(s[j]) && j < s.length(); j++)
                    {
                        tempc *= 10;
                        tempc += (s[j] - '0');
                    }
                    char a[10] = {s[i]};
                    a[1] = '\0';
                    mp[string(a)] += tempc;
                }

                //小写跟数字
                if (islower(s[i]) && isdigit(s[i + 1]))
                {
                    int tempc = s[i + 1] - '0';
                    for (int j = i + 2; isdigit(s[j]) && j < s.length(); j++)
                    {
                        tempc *= 10;
                        tempc += (s[j] - '0');
                    }
                    char a[10];
                    a[0] = s[i - 1];
                    a[1] = s[i];
                    a[2] = '\0';
                    string name(a);
                    mp[name] += tempc;
                }

                //小写跟大写
                if (islower(s[i]) && isupper(s[i + 1]))
                {

                    char a[10];
                    a[0] = s[i - 1];
                    a[1] = s[i];
                    a[2] = '\0';
                    string name(a);
                    mp[name] += 1;
                }
            }

            //大写结尾
            if (isupper(s[s.length() - 1]))
            {
                char a[10] = {s[s.length() - 1]};
                a[1] = '\0';
                mp[string(a)] += 1;
            }

            //小写结尾
            if (islower(s[s.length() - 1]))
            {
                char a[10];
                a[0] = s[s.length() - 2];
                a[1] = s[s.length() - 1];
                a[2] = '\0';
                string name(a);
                mp[name] += 1;
            }

            for (auto &m : mp)
            {
                m.second = m.second * c;
                resmp[m.first] += m.second;
            }
        }
        else
        {

            return resmp;
        }
    }
    return resmp;
}

void judge()
{
    int index = equ.find("=", 0);
    string left = equ.substr(0, index);
    string right = equ.substr(index + 1);
    auto s1 = split(left);
    auto s2 = split(right);
    auto m1 = check(s1);
    auto m2 = check(s2);
    if (m1.empty() || m2.empty())
    {
        cout << "N" << endl;
    }
    else
    {
        if (m1 == m2)
        {
            cout << "Y" << endl;
        }
        else
        {
            cout << "N" << endl;
        }
    }
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> equ;
        judge();
    }
    // system("pause");
    return 0;
}